package com.achun.getcouponleague.ui.adapter;

import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.achun.getcouponleague.Bean.Red_Packets;
import com.achun.getcouponleague.R;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Red_packetListAdapter extends RecyclerView.Adapter<Red_packetListAdapter.innerholder> {
    private List<Red_Packets.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> map_data=new ArrayList<>();
    @NonNull
    @Override
    public innerholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.red_packet_item, parent, false);
        return new innerholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull innerholder holder, int position) {
        Red_Packets.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean = map_data.get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.OnItemCLick(mapDataBean);
            }
        });

        holder.setDatas(mapDataBean);
    }

    @Override
    public int getItemCount() {
        return map_data.size();
    }

    public void setDatas(List<Red_Packets.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> map_data) {
        this.map_data.clear();
        this.map_data.addAll(map_data);
        notifyDataSetChanged();
    }

    public void addDatas(List<Red_Packets.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> map_data) {
        int size = this.map_data.size();
        this.map_data.addAll(map_data);
        notifyItemRangeChanged(size,map_data.size());
    }

    public class innerholder extends RecyclerView.ViewHolder {
        @BindView(R.id.red_cover_view)
        public ImageView red_cover_view;
        @BindView(R.id.tv_title)
        public TextView tv_title;
        @BindView(R.id.old_prece)
        public TextView old_price;
        @BindView(R.id.new_price)
        public TextView new_price;
        public innerholder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void setDatas(Red_Packets.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean mapDataBean) {
            Glide.with(itemView.getContext()).load("https:"+mapDataBean.getPict_url()).into(red_cover_view);
            tv_title.setText(mapDataBean.getTitle());
            String originPrice = mapDataBean.getZk_final_price();
            float finalPrice=Float.parseFloat(originPrice)-mapDataBean.getCoupon_amount();
            old_price.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            old_price.setText("￥"+originPrice);
            new_price.setText("券后价："+String.format("%.2f",finalPrice));
        }
    }
    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
    public interface OnItemClickListener{
        void OnItemCLick(Red_Packets.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean bean);
    }
}
